import sys
import requests
sys.path.append("../")
from model.ExamModel import *
from model.CommonDb import *

class BaseService:
    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,'
                  'application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'max-age=0',
        'Connection': 'close',
        'Cookie': 'timezoneOffset=28800,0; _ga=GA1.2.2068924460.1568258364; browserid=1637567403399993326; '
                  'sessionid=0be65a169d686e464855602e; _gid=GA1.2.614169347.1590475272; '
                  'steamMachineAuth76561199007171441=CA87107A153BDC24FE6D54504A124395F9C36243; '
                  'deep_dive_carousel_focused_app=673750; deep_dive_carousel_method=gems; '
                  'steamCountry=HK%7C413cfd508f5a328ba1679f9252231a8e; '
                  'app_impressions=377530@1_7_7_topsellers_150_3|1245200@1_7_7_topsellers_150_3|632360'
                  '@1_7_7_topsellers_150_3|1245180@1_7_7_topsellers_150_3|1298590@1_7_7_topsellers_150_3|285900'
                  '@1_7_7_topsellers_150_3|632470@1_7_7_topsellers_150_2|813780@1_7_7_topsellers_150_2|1100600'
                  '@1_7_7_topsellers_150_2|1293820@1_7_7_topsellers_150_2|952860@1_7_7_topsellers_150_2|1209110'
                  '@1_7_7_topsellers_150_2|1015610@1_7_7_topsellers_150_3|1238440@1_7_7_topsellers_150_3|1042550'
                  '@1_7_7_topsellers_150_3|466560@1_7_7_topsellers_150_3|602960@1_7_7_topsellers_150_3|457140'
                  '@1_7_7_topsellers_150_3|793460@1_7_7_topsellers_150_3|512540@1_7_7_topsellers_150_3|619150'
                  '@1_7_7_topsellers_150_3|1100410@1_7_7_topsellers_150_3|1088780@1_7_7_topsellers_150_3|1100420'
                  '@1_7_7_topsellers_150_3|1201360@1_7_7_topsellers_150_3|642280@1_7_7_topsellers_150_3|673950'
                  '@1_7_7_topsellers_150_3|613830@1_7_7_topsellers_150_3|636480@1_7_7_topsellers_150_3|387990'
                  '@1_7_7_topsellers_150_3|414700@1_7_7_topsellers_150_3|382310@1_7_7_topsellers_150_3|1162520'
                  '@1_7_7_topsellers_150_2|289070@1_7_7_topsellers_150_2|307690@1_7_7_topsellers_150_2|973760'
                  '@1_7_7_topsellers_150_2|1273370@1_5_9__405; recentapps=%7B%22578080%22%3A1590476900%7D',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-User': '?1',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20210911 Firefox/52.0'
    }

    def __init__(self):
        pass

    def translateDfcfCode(self,code):
        subfix = code[:1]
        if subfix == "6":
            code2 = "1." + code
        else:
            code2 = "0." + code

        return code2

    def getCode(self,code,type):
        subfix = code[:1]
        if subfix == "6":
            code1 = code + ".SH"
            code2 = "SH" + code
        else:
            code1 = code + ".SZ"
            code2 = "SZ" + code
        if type == 1:
            return code1
        else:
            return code2

    def getColumns(self,table):
        self.db = CommonDb(table,"stock")
        return self.db.getColumns()

    def history(self,code, start, end):
        """

        :param code:  股票代码
        :param start: 开始日期  20210101
        :param end:    结束日期  20210102
        :return:
        """
        try:
            url = 'http://q.stock.sohu.com/hisHq?code=cn_{}&start={}&end={}'
            rsp = requests.get(url.format(code, start, end), timeout=5)
            if isinstance(rsp.json(), dict):
                # 需要list
                return []
            if 'hq' not in rsp.json()[0]:  # 需要有 hq字段 否则[0]['hq']报错
                return []
            datas = rsp.json()[0]['hq']
            """
            日期,
            今日开盘价,
            今日收盘价,
            涨跌幅度，
            涨跌百分比,
            今日最低价,
            今日最高价，
            今日成交股票手数,
            今日成交金额(单位：万)
            换手率
            """
            return datas
        except:
            return []

    def translateDfToMap(self,df):
        dfVal = df.to_dict()
        newmap = {}
        for key in dfVal["item"].keys():
            newmap[dfVal["item"][key]] = dfVal["value"][key]
        return newmap


